package com.qf.BaseController;

import com.qf.dao.UserDaoImpl;
import com.qf.pojo.User;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Constants;
import com.qf.utils.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author Wy
 * @version V1.0
 * @Project javaWeb项目
 * @Package com.qf.BaseController
 * @Description:
 * @Date 2022/5/23 15:10
 */
@WebServlet("/user")
public class UserController extends BaseServlet {
    /**
     * 用户注册，检验用户是否可用
     * @param request
     * @param response
     * @throws IOException
     */


    public  void check(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        UserServiceImpl userService = new UserServiceImpl(new UserDaoImpl());
        boolean check = userService.checkduUser(username);
        //3.将信息传递给页面
        if(check){
            response.getWriter().write(Constants.NOT_HAS_USER);

        }else {
            response.getWriter().write(Constants.HAS_USER);
        }
    }

    /**
     * 消费用户登录
     * @param request
     * @param response
     * @return
     */


    public String login(HttpServletRequest request, HttpServletResponse response){
        //1.获取
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String auto = request.getParameter("auto");
        //5.判断用户名，密码，验证码是否为空 如果为空定位到登录界面

        if(!StrUtils.empty(username)||!StrUtils.empty(password)||!StrUtils.empty(code)){
            request.setAttribute("msg","用户名，验证码不能为空，密码不能为空");
            //跳转到登录界面
            return  "forward:/login.jsp";
        }
        //
        String seesionCode = (String) request.getSession().getAttribute("code");
        if(!code.equalsIgnoreCase(seesionCode)){
            request.setAttribute("userName",username);
            request.setAttribute("password",password);
            request.setAttribute("msg","验证码错误");
            //跳转到登录界面
            return  "forward:/login.jsp";
        }
        //7.调用service完成用户名密码的校验
        UserServiceImpl userService = new UserServiceImpl(new UserDaoImpl());
        User user = userService.logins(username, password);
        //8.判断用户名密码是否正确
    if(user==null){
        request.setAttribute("userName",username);
        request.setAttribute("password",password);
        request.setAttribute("msg","验证码错误");
        //跳转到登录界面
        return  "forward:/login.jsp";
    }
    //9.登录成功，将用户信息放入tomcat的session 会话中携带
        request.getSession().setAttribute("loginUser",user);

    //10.判断用户是否要自动登录，进行自动登录设置
        if("on".equals(auto)){
            Cookie cookie = new Cookie(Constants.AUTO_NAME, user.getUsername());
            //设置这个cookie的保存路径
            cookie.setPath("/");
            //设置保存时间
            cookie.setMaxAge(60*60*24*14);
            //通过响应将cookie数据保存写回到浏览器
            response.addCookie(cookie);

        }
        return  "redirect:/goods?method=index";





    }

    /**
     * 消费者新用户注册
      * @param request
     * @param response
     * @return
     */
    public String register(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //1. 获取页面传入的所有参数, key就是页面input框的name属性, value就是input框中输入的值
        Map<String, String[]> parameterMap = request.getParameterMap();
        //2. 将参数封装成User对象
        //创建用户对象
        User user = new User();
        //利用beanUtils工具类, 将map中的数据全部自动set到user对象中
        //使用要求: user对象的属性名必须和页面input框的name属性一致, 实体类必须实现Serializable序列化接口

        BeanUtils.populate(user,parameterMap);

        //3. 调用service, 将用户对象保存到mysql数据库的用户表中
        UserServiceImpl userService = new UserServiceImpl(new UserDaoImpl());
        int count = userService.createUser(user);
        //4. 跳转到注册成功页面
        if(count>0){
            //注册成功
            return  "redirect:"+request.getContextPath()+"/registerSuccess.jsp";
        }else {
            //注册失败
            return  "redirect:"+request.getContextPath()+"/register.jsp";
        }

    }

}
